package com.sy.service.impl;

import com.alibaba.fastjson.JSON;
import com.sy.dto.Page;
import com.sy.entity.*;
import com.sy.service.ArticleService;
import com.sy.utils.RedisOperations;
import com.sy.vo.Aggregate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.sy.constant.BusinessConstant.*;

@Service
public class ArticleServiceImpl extends BaseServiceImpl implements ArticleService {

    @Autowired
    private RedisOperations redisOperations;

    @Override
    public List<Type> getAllType() {
        return articleMapper.getTypeList();
    }

    @Override
    public Type getTypeByTypeId(Integer typeid) {
        return articleMapper.getTypeById(typeid);
    }

    @Override
    public Message getMessageByTypeId(Integer messageid) {
        return articleMapper.getMessageById(messageid);
    }

    @Override
    public Page<Message> getMessageListByaggregate(Aggregate carpototion) {

        String key=REDIS_ARTICLE_KEY+carpototion.getArticletypeid()+carpototion.getPageNo();
        System.out.println(key);
//        if(redisOperations.exists(key)){
//            Page<Message> page= JSON.parseObject(redisOperations.get(key), Page.class);
//            List<Message>list=page.getData();
//
//            return JSON.parseObject(redisOperations.get(key), Page.class);
//        }else{
            Page<Message> page = new Page<>();
            Integer pageNo = carpototion.getPageNo();
            page.setPageNo(pageNo == null ? DEFAULT_PAGE_NO : pageNo);
            page.setPageSize(DEFAULT_PAGE_SIZE);
            Integer typeid=carpototion.getArticletypeid();
            Integer hotid=null;
            if(typeid==1){
                hotid=1;
            }
            Long total = articleMapper.getMessagesCountByAggregate(typeid,hotid);
            page.setTotal(total);
            page.setPageCount(total %DEFAULT_PAGE_SIZE == 0 ? total / DEFAULT_PAGE_SIZE : total / DEFAULT_PAGE_SIZE + 1);
            Integer start = (pageNo-1 ) *DEFAULT_PAGE_SIZE;

            page.setData(articleMapper.getMessagesByAggregate(typeid,start));
            redisOperations.set(key, JSON.toJSONString(page));
            return page;
//        }
    }
    @Override
    public List<Message> getMessageListById(Integer typeid) {
        System.out.println(typeid);
        List<Message> list=articleMapper.getMessageListByTypeId(typeid);
        return list;

    }
    @Override
    public List<Trafficsigns> getTrafficSignsList() {
        List<Trafficsigns> list=articleMapper.getTrafficSignsList();
        return list;
    }

    @Override
    public List<Warn> getWarnList(Integer trafficsignsid) {

        return articleMapper.getWarnListByTrafficId(trafficsignsid);
    }

    @Override
    public List<Trafficpiture> getTrafficPictureListByWarnId(Integer trafficsignsid, Integer warnid) {

        return articleMapper.getPictureList(trafficsignsid,warnid);
    }

    @Override
    public Warn getWarnByTrafficSignsId(Integer trafficsignsid) {

        Warn warn=articleMapper.getWarnByTrafficSingsId(trafficsignsid);

        return warn;
    }
    @Override
    public void changeViewNumber(Integer messageid) {
        articleMapper.changeViewNumber(messageid);
    }
}
